package com.tbit.uqbike.push.util;

import com.alibaba.fastjson.JSON;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 阿里语音寻呼
 *
 * @author Leon
 * 2020年4月16日 下午2:43:41
 */
@Component
public class CallHttpClient {
    private final static Logger log = Logger.getLogger(CallHttpClient.class);
    private static HttpClient client = null;
    private static String appcode = "99e8de0e113040e4b5b5dc31bed2b817";
    private static String tpl_id = "TP20052810";

    /**
     * 维持单session
     */
    public static HttpClient getClient() {
        if (client == null) {
            MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
            client = new HttpClient(connectionManager);
            /**设置编码*/
            client.getParams().setContentCharset("UTF-8");
            /**请求超时*/
            client.getHttpConnectionManager().getParams().setConnectionTimeout(1000);
            /**读取超时*/
            client.getHttpConnectionManager().getParams().setSoTimeout(5000);
        }
        return client;
    }

    /**
     * 语音寻呼
     *
     * @param phone
     * @return
     */
    public static Boolean call(String phone) {
        HttpClient client = getClient();
        PostMethod post = null;
        try {
            String url = "http://yuyin2.market.alicloudapi.com/dx/voice_notice";
            post = new PostMethod(url);
            post.setRequestHeader("Authorization", "APPCODE " + appcode);

            NameValuePair phonePair = new NameValuePair("phone", phone);
            NameValuePair tplPair = new NameValuePair("tpl_id", tpl_id);
            post.setRequestBody(new NameValuePair[]{phonePair, tplPair});
            client.executeMethod(post);
            String result = post.getResponseBodyAsString();
            Integer httpCode = post.getStatusCode();
            if (httpCode != 200) {
                log.error("Code=" + httpCode + ",Error=" + post.getResponseHeader("X-Ca-Error-Code"));
                log.error("Code=" + httpCode + ",Error=" + post.getResponseHeader("X-Ca-Error-Message"));
            }
            log.info("回复=" + result);
            String return_code = JSON.parseObject(result).getString("return_code");
            if ("00000".equals(return_code)) {
                return true;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            post.releaseConnection();
        }

        return false;
    }

    /**
     * 语音寻呼可配置
     *
     * @param phone
     * @return
     */
    public static Boolean callByModelId(String phone, String appCode, String tolId) {
        HttpClient client = getClient();
        PostMethod post = null;
        try {
            String url = "http://yuyin2.market.alicloudapi.com/dx/voice_notice";
            post = new PostMethod(url);
            post.setRequestHeader("Authorization", "APPCODE " + appCode);

            NameValuePair phonePair = new NameValuePair("phone", phone);
            NameValuePair tplPair = new NameValuePair("tpl_id", tolId);
            post.setRequestBody(new NameValuePair[]{phonePair, tplPair});
            client.executeMethod(post);
            String result = post.getResponseBodyAsString();
            Integer httpCode = post.getStatusCode();
            if (httpCode != 200) {
                log.error("Code=" + httpCode + ",Error=" + post.getResponseHeader("X-Ca-Error-Code"));
                log.error("Code=" + httpCode + ",Error=" + post.getResponseHeader("X-Ca-Error-Message"));
            }
            log.info("回复=" + result);
            String return_code = JSON.parseObject(result).getString("return_code");
            if ("00000".equals(return_code)) {
                return true;
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        } finally {
            post.releaseConnection();
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(callByModelId("15079017029", "32a34040d96f4ef3ad6cc563fc03a492", "TP20052810"));
    }

    @Value("${appcode:99e8de0e113040e4b5b5dc31bed2b817}")
    public void setAppcode(String appcode) {
        log.info("set CallHttpClient.appcode" + " = " + appcode);
        CallHttpClient.appcode = appcode;
    }

    @Value("${tpl_id:TP20052810}")
    public void setTpl_id(String tpl_id) {
        log.info("set CallHttpClient.tpl_id" + " = " + tpl_id);
        CallHttpClient.tpl_id = tpl_id;
    }
}
